package com.train.batch.facade;

import cn.hutool.core.bean.BeanUtil;
import com.train.batch.dto.AddJobDTO;
import com.train.batch.dto.EditJobDTO;
import com.train.batch.dto.JobPageDTO;
import com.train.batch.req.AddJobReq;
import com.train.batch.req.EditJobReq;
import com.train.batch.req.JobPageReq;
import com.train.batch.resp.JobPageResp;
import com.train.batch.service.BatchService;
import com.train.common.resp.CommonResp;
import com.train.common.resp.PageResp;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("job")
public class BatchFacade {

    private final BatchService batchService;

    @PostMapping("add")
    public CommonResp addJob(@RequestBody AddJobReq addJobReq) {
        AddJobDTO addJobDTO = BeanUtil.copyProperties(addJobReq, AddJobDTO.class);
        batchService.addJob(addJobDTO);
        return CommonResp.ok();
    }

    @DeleteMapping("remove/{jobName}/{groupName}")
    public CommonResp deleteJob(@PathVariable String jobName, @PathVariable String groupName) {
        batchService.deleteJob(jobName, groupName);
        return CommonResp.ok();
    }

    @PostMapping("edit")
    public CommonResp updateJob(@RequestBody EditJobReq editJobReq) {
        EditJobDTO editJobDTO = BeanUtil.copyProperties(editJobReq, EditJobDTO.class);
        batchService.updateJob(editJobDTO.getJobName(), editJobDTO.getGroupName(), editJobDTO.getCronExpress());
        return CommonResp.ok();
    }

    @PostMapping("page")
    public CommonResp pageList(@RequestBody JobPageReq jobPageReq) {
        JobPageDTO jobPageDTO = BeanUtil.copyProperties(jobPageReq, JobPageDTO.class);
        PageResp<JobPageResp> jobPageResp = batchService.pageList(jobPageDTO);
        return CommonResp.ok(jobPageResp);
    }

    @PostMapping("pause/{jobName}/{groupName}")
    public CommonResp pauseJob(@PathVariable String jobName, @PathVariable String groupName) {
        batchService.pauseJob(jobName, groupName);
        return CommonResp.ok();
    }

    @PostMapping("resume/{jobName}/{groupName}")
    public CommonResp resumeJob(@PathVariable String jobName, @PathVariable String groupName) {
        batchService.resumeJob(jobName, groupName);
        return CommonResp.ok();
    }

    @PostMapping("run/{jobName}/{groupName}")
    public CommonResp runJob(@PathVariable String jobName, @PathVariable String groupName) {
        batchService.runJob(jobName, groupName);
        return CommonResp.ok();
    }
}
